package com.oocl.euc.util;

import java.lang.reflect.Field;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;

import javax.persistence.Id;

public class ModelUtils {

	public static Type[] getGenericClasses(Class c) {
		if (c.getGenericSuperclass() instanceof ParameterizedType) {
			return ((ParameterizedType) c.getGenericSuperclass())
					.getActualTypeArguments();
		} else {
			return null;
		}
	}
	
	public static Class getUniquenessGenericClass(Class c) {
		Type[] types = getGenericClasses(c);
		if(types != null) {
			return (Class)types[0];
		}else {
			return null;
		}
	}

	public static String getPrimaryKeyName(Class modelClass) {
		Field field = getPrimaryKeyField(modelClass);
		if (field != null) {
			return field.getName();
		} else {
			return null;
		}
	}

	public static Field getPrimaryKeyField(Class modelClass) {
		Field[] fields = modelClass.getDeclaredFields();
		for (Field f : fields) {
			if (f.isAnnotationPresent(Id.class)) {
				return f;
			}
		}
		return null;
	}
}
